<template>
  <div class="list-table">
    <el-table ref="multipleTable" v-loading="loading" :row-key="getRowKey" tooltip-effect="dark" :data="fundFpLossDealList" border @selection-change="getCheckedData">
      <el-table-column
        v-model="checkedChange"
        reserve-selection
        type="selection"
        width="55"
      />
      <el-table-column :label="$t('项目名称')" align="center" prop="dealName" />
      <el-table-column
        :label="$t('投资本金') + $t('金额单位')"
        align="center"
        prop="actualPaymentMoney"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.actualPaymentMoney, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('已分配本金') + $t('金额单位')"
        align="center"
        prop="principalCost"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.principalCost, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('理论Hurdle') + $t('金额单位')"
        align="center"
        prop="hurdleTheory"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.hurdleTheory, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('实际Hurdle') + $t('金额单位')"
        align="center"
        prop="priorityReturn"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.priorityReturn, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('理论追赶条款') + $t('金额单位')"
        align="center"
        prop="clauseTheory"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.clauseTheory, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('实际追赶条款') + $t('金额单位')"
        align="center"
        prop="chaseTerms"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.chaseTerms, 'w') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <el-button @click="cancel">{{ $t("取消") }}</el-button>
      <el-button type="primary" @click="confirmChecked">{{ $t('确认') }}</el-button>
    </div>
  </div>
</template>

<script>
import { listFundFpLossDeal, getCheckedList, saveChecked, allocationCalculate } from '@/api/fund/fp/fundFpLossDeal'
export default {
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 待补亏项目信息表格数据
          fundFpLossDealList: [],
          // 弹出层标题
          title: '待补亏项目信息',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              fpId: undefined,
              dealName: undefined
          },
          // 选中的ID
          checkedChange: [],
          // 表单参数
          form: {
            id: undefined,
            fundFpLossDealList: []
          },
          // 表单校验
          rules: {},
          checkedObj: []
      }
  },
  methods: {
    getRowKey(row) {
      return row.id
    },
    getFundLossDealList(row) {
      this.loading = true
      this.queryParams.fpId = row.id
      this.checkedObj = []
      this.fundFpLossDealList = []
      listFundFpLossDeal(row.id).then(response => {
        this.fundFpLossDealList = response.rows
        this.total = response.total
        this.initCheckedData()
      })
    },
    initCheckedData() {
      this.$refs.multipleTable.clearSelection()
      getCheckedList(this.queryParams.fpId).then(response => {
        response.rows.forEach(item => {
          this.fundFpLossDealList.forEach((dd) => {
            if (item.dealId === dd.dealId) {
              this.$refs.multipleTable.toggleRowSelection(dd)
            }
          })
        })
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.lossOpen = false
    },
    // 点击确认
    confirmChecked() {
      this.form.id = this.queryParams.fpId
      this.form.fundFpLossDealList = []
      this.fundFpLossDealList.forEach(fpLoss => {
          this.checkedObj.forEach(item => {
            if (fpLoss.dealId === item.fpLoss.dealId) {
              this.form.fundFpLossDealList.push(item.fpLoss)
            }
          })
      })
      saveChecked(this.form).then(() => {
        this.loading = true
        allocationCalculate(this.queryParams.fpId).then(() => {
          this.loading = false
          this.cancel()
          this.msgSuccess(this.$t('分配计算成功'))
        })
      })
    },
    // 选中数据
    getCheckedData(val) {
      this.checkedObj = []
      if (val !== null && val !== undefined) {
        val.forEach(item => {
          if (item !== undefined) {
            var row = {
              dealId: item.dealId,
              fpId: this.queryParams.fpId
            }
            this.checkedObj.push({ fpLoss: row })
          }
        })
      }
    }
  }
}
</script>
